package cn.iocoder.yudao.module.wine.service.countnameconfig;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.wine.controller.admin.countnameconfig.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.countnameconfig.CountNameConfigDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.countnameconfig.CountNameConfigMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 统计名称配置 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class CountNameConfigServiceImpl implements CountNameConfigService {

    @Resource
    private CountNameConfigMapper countNameConfigMapper;

    @Override
    public Long createCountNameConfig(CountNameConfigSaveReqVO createReqVO) {
        // 插入
        CountNameConfigDO countNameConfig = BeanUtils.toBean(createReqVO, CountNameConfigDO.class);
        countNameConfigMapper.insert(countNameConfig);
        // 返回
        return countNameConfig.getId();
    }

    @Override
    public void updateCountNameConfig(CountNameConfigSaveReqVO updateReqVO) {
        // 校验存在
        validateCountNameConfigExists(updateReqVO.getId());
        // 更新
        CountNameConfigDO updateObj = BeanUtils.toBean(updateReqVO, CountNameConfigDO.class);
        countNameConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteCountNameConfig(Long id) {
        // 校验存在
        validateCountNameConfigExists(id);
        // 删除
        countNameConfigMapper.deleteById(id);
    }

    private void validateCountNameConfigExists(Long id) {
        if (countNameConfigMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public CountNameConfigDO getCountNameConfig(Long id) {
        return countNameConfigMapper.selectById(id);
    }

    @Override
    public PageResult<CountNameConfigRespVO> getCountNameConfigPage(CountNameConfigPageReqVO pageReqVO) {
        return countNameConfigMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CountNameConfigRespVO> getCountNameConfigList(CountNameConfigListReqVO reqVO) {

        List<CountNameConfigDO> countNameConfigDOS = countNameConfigMapper.selectList(new LambdaQueryWrapperX<CountNameConfigDO>()
                .eq(CountNameConfigDO::getFragranceType, reqVO.getFragranceType())
                .eq(CountNameConfigDO::getVisible, 1)
                .orderByAsc(CountNameConfigDO::getSort));
        return BeanUtils.toBean(countNameConfigDOS,CountNameConfigRespVO.class);
    }

}
